package ltd.helper;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * spring操作助手
 */
public class SpringHelper {

	private static ApplicationContext ctx = null;
	
	private SpringHelper() {
		super();
	}
	
	public static void init(){
		if(ctx == null){
			ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		}
	}
	
	/**
	 * 获取applicationContext
	 */
	public static ApplicationContext getSpringCtx() {
		init();
		return ctx;
	}
	
	/**
	 * 获取指定的Bean类
	 * @param beanName Bean类的名称
	 * @return Object对象，需自行强制转换类型
	 */
	public static Object getBean(String beanName){
		init();
		try {
			return ctx.getBean(beanName);
		} catch (Exception e) {
			System.err.println("== 获取Bean：[" + beanName + "] 出错!!! ==");
			e.printStackTrace();
		}
		return null;
	}
	
}
